﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Schedule
{
    public partial class CourseForm : Form
    {
        private DataTableManage CourseDataTableManage = new DataTableManage(new DataTable("subject"));
        private CourseDataManage courseDataManage = new CourseDataManage();
        private DDLManage DeadlineManage = new DDLManage();
        public static bool ifColor = true;

        public CourseForm()
        {
            InitializeComponent();
            courseDataManage.dataInit();//初始化数据库
            CourseDataTableManage.dataTableInit();//初始化课程表
            CourseDataTableManage.dataTableFill();

            this.dataGridView1.DataSource = CourseDataTableManage.CourseDataTable;//绑定课程表数据
            DDLManage.updateDDL += new updateDDLHandle(fillColor);//绑定更新颜色事件
            DeadlineManage.dataInit();//初始化ddl数据
            

        }

        private void addCourseBtn_Click(object sender, EventArgs e)
        {
            AddCourseForm addCourseForm = new AddCourseForm();
            addCourseForm.Show();
            addCourseForm.SubmitClick += CourseDataTableManage.dataTableFill;
        }

        private void button1_Click(object sender, EventArgs e)
        {//删除课程
            
            if(!courseDataManage.deleteCourse(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex + 1))
                MessageBox.Show("删除失败，无课程");
            CourseDataTableManage.dataTableFill();//更新课程表
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            courseDataManage.getCourseInfo(e.ColumnIndex, e.RowIndex + 1, out string courseID, out string courseName);
            if (courseID == "")
                MessageBox.Show("无课程");
            else
                new CheckInfoForm(courseID, courseName).Show();


        }

        private void button2_Click(object sender, EventArgs e)
        {
            AddDeadlineForm addDeadlineForm = new AddDeadlineForm(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex + 1);
            addDeadlineForm.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (ifColor)
            {
                ifColor = false;
                button3.Text = "开启提醒";
            }
            else
            {
                ifColor = true;
                button3.Text = "关闭提醒";
            }
            fillColor();
        }

        public void fillColor()
        {
            DataGridView dataGridView = this.dataGridView1;
            List<Deadline> deadlines = DeadlineManage.deadlines;
            if (CourseForm.ifColor == false)
            {
                for (int i = 0; i < 8; i++)
                    dataGridView.Columns[i].DefaultCellStyle.BackColor = System.Drawing.Color.White;
                return;
            }
            string color1 = "#c0ebd7", color2 = "#edd1d8", color3 = "#cca4e3", color4 = "#ef7a82";
            deadlines.Sort((d1, d2) => (d2.DeadlineDate - d1.DeadlineDate).Days);
            DateTime currentDate = DateTime.Now;
            int spanDays = 0, weekday = 0;
            if (dataGridView != null)
            {
                for (int i = 0; i < 8; i++)
                    dataGridView.Columns[i].DefaultCellStyle.BackColor = System.Drawing.Color.White;
                foreach (Deadline ddl in deadlines)
                {
                    weekday = Course.courses.Find(c => c.CourseID == ddl.CourseID).Weekday;
                    spanDays = (ddl.DeadlineDate - currentDate).Days;

                    if (spanDays > 7)
                        dataGridView.Columns[weekday].DefaultCellStyle.BackColor = colorHx16toRGB(color1);
                    else if (spanDays >= 4)
                        dataGridView.Columns[weekday].DefaultCellStyle.BackColor = colorHx16toRGB(color2);
                    else if (spanDays >= 2)
                        dataGridView.Columns[weekday].DefaultCellStyle.BackColor = colorHx16toRGB(color3);
                    else
                        dataGridView.Columns[weekday].DefaultCellStyle.BackColor = colorHx16toRGB(color4);
                }
            }

        }

        public static System.Drawing.Color colorHx16toRGB(string strHxColor)
        {
            try
            {
                if (strHxColor.Length == 0)
                {//如果为空
                    return System.Drawing.Color.FromArgb(0, 0, 0);//设为黑色
                }
                else
                {//转换颜色
                    return System.Drawing.Color.FromArgb(System.Int32.Parse(strHxColor.Substring(1, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(strHxColor.Substring(3, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(strHxColor.Substring(5, 2), System.Globalization.NumberStyles.AllowHexSpecifier));
                }
            }
            catch
            {//设为黑色
                return System.Drawing.Color.FromArgb(0, 0, 0);
            }
        }
    }
}
